﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smartec.Accounts.Camel.AMS.Model.Entities;
using Smartec.Accounts.Camel.AMS.Model.Repository;
using Smartec.Accounts.Camel.AMS.Model.Facades;
using Smartec.Framework.Common;
using System.IO;

namespace Smartec.Accounts.Camel.AMS.WebApp.Controllers
{
    [Authorize]
    public class BannerController : Controller
    {
        //
        // GET: /Banner/

        public ActionResult Index()
        {
            List<Banner> banners = BannerFacade.GetAllBanners();
            return View(banners);
        }

        //
        // GET: /Banner/Details/5

        public ActionResult Details(int id)
        {
            var banner = BannerFacade.GetBannerById(id);
            return View(banner);
        }

        //
        // GET: /Banner/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Banner/Create

        [HttpPost]
        public ActionResult Create(Banner banner,FormCollection formcollection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                
                {
                    string bannerImagePath =  Server.MapPath("/Banner/");
                    HttpPostedFileBase file = Request.Files["BannerImageUploader"];
                    string fileName = "";
                    if (file.ContentLength > 0)
                    {
                        fileName = Guid.NewGuid() + "-" + Path.GetFileName(file.FileName);
                        string filePath = Path.Combine(bannerImagePath, fileName);
                        file.SaveAs(filePath);

                        banner.Image = fileName;
                    }

                    BannerFacade.AddBanner(banner);
                    BannerFacade.Save();
                    return RedirectToAction("Index");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Banner/Edit/5
 
        public ActionResult Edit(int id)
        {
            var banner = BannerFacade.GetBannerById(id);
            return View(banner);
        }

        //
        // POST: /Banner/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Banner banner)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    string bannerImagePath = Server.MapPath("/Banner/");
                    HttpPostedFileBase file = Request.Files["BannerImageUploader"];
                    string fileName = "";
                    if (file.ContentLength > 0)
                    {
                        fileName =Guid.NewGuid() + "-" + Path.GetFileName(file.FileName);
                        string filePath = Path.Combine(bannerImagePath, fileName);
                        file.SaveAs(filePath);

                        banner.Image = fileName;
                    }

                    BannerFacade.UpdateBanner(banner);
                    BannerFacade.Save();
                    return RedirectToAction("Index");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Banner/Delete/5
 
        public ActionResult Delete(int id)
        {
            var banner = BannerFacade.GetBannerById(id);
            return View(banner);
        }

        //
        // POST: /Banner/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                BannerFacade.DeleteBannerById(id);
                BannerFacade.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
